package com.ht.ui;

import javax.swing.*;
import java.awt.*;

/**
 * Created by IntelliJ IDEA.
 * Author: Wangjiaxiang
 * Date: 2011-8-14
 */
public class DynamicLabel extends JLabel {
    private int step = 1;
    private int startIndex = 0;
    private String tempTxt;
    private String originalTxt;
    private Color redColor = Color.RED;
    private Color blackColor = Color.BLACK;
    private Color currentColor = redColor;
    private Thread thread;
    private boolean isStop = false;
    private boolean isPause = false;

    public DynamicLabel(String text) {
        super(text, SwingConstants.RIGHT);
        setFont(new Font("Dialog", Font.BOLD, 18));
        originalTxt = text;
        tempTxt = text;
        thread = new Thread() {
            public void run() {
                while (!isStop) {
                    if (!isPause)
                        doTask();
                    else {
                        originalTxt = "";
                        tempTxt = "";
                        setForeground(currentColor);
                        step = 1;
                        startIndex = 0;
                    }
                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
    }

    private void doTask() {
        if (step > tempTxt.length()) {
            step = 1;
            startIndex = 0;
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        try {
            if (step > 45) {
                startIndex++;
                String tmp = tempTxt.substring(startIndex, startIndex + 1);
                if (tmp.equals(",") || tmp.equals(":"))
                    startIndex++;
            }
            originalTxt = tempTxt.substring(startIndex, step);
        } catch (StringIndexOutOfBoundsException e) {
            System.out.println(e.getMessage());
            step = 0;
            startIndex = 0;
        }
        step++;
        setForeground(currentColor);
        if (currentColor == redColor)
            currentColor = blackColor;
        else
            currentColor = redColor;
    }

    public String getToolTipText() {
        return tempTxt;
    }

    public DynamicLabel() {
        this("");
        setPreferredSize(new Dimension(730, 25));
    }

    public String getText() {
        return originalTxt;
    }

    public String getTempText() {
        return tempTxt;
    }

    public void setText(String text) {
        if (text.trim().equals(""))
            return;
        originalTxt = text;
        tempTxt = text;
        setPause(false);
        step = 1;
        startIndex = 0;
        if (!thread.isAlive())
            thread.start();
    }

    public void setPause(boolean statu) {
        isPause = statu;
    }


    public void setTempTxt(String tempTxt) {
        this.tempTxt = tempTxt;
    }


    public void setOriginalTxt(String originalTxt) {
        this.originalTxt = originalTxt;
    }
}
